package com.jader.myviews;

import com.google.gson.Gson;
import com.jader.beans.Evalute;
import com.jader.beans.Goods;
import com.jader.util.JaderApplication;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.zlp.jader.R;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;

@SuppressLint("CommitPrefEdits")
public class PingJiaPopWindow implements OnDismissListener, OnClickListener {

	private EditText editText;
	private Button commitButton;
	private PopupWindow popupWindow;
	private OnItemClickListener listener;
	private Context context;
	private Goods goods;
	private Evalute evalute;
	private String url = JaderApplication.getIP() + "/Jader/client";

	public PingJiaPopWindow(Context context) {
		this.context = context;
		View view = LayoutInflater.from(this.context).inflate(
				R.layout.adapter_pingjia, null);
		editText = (EditText) view.findViewById(R.id.pingjia_input);
		commitButton = (Button) view.findViewById(R.id.commit_pingjia);
		popupWindow = new PopupWindow(view, LayoutParams.MATCH_PARENT,
				LayoutParams.WRAP_CONTENT);
		popupWindow.setAnimationStyle(R.style.AnimationFade);
		popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
		popupWindow.setOnDismissListener(this);
		popupWindow.setOutsideTouchable(true);
		commitButton.setOnClickListener(this);
	}

	public interface OnItemClickListener {
		public void onClickOKPop();
	}

	public boolean isShowing() {
		return popupWindow.isShowing();
	}

	public void setOnItemClickListener(OnItemClickListener listener) {
		this.listener = listener;
	}

	@Override
	public void onDismiss() {
		listener.onClickOKPop();

	}

	public void showAsDropDown(View parent) {
		popupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
		popupWindow.setFocusable(true);
		popupWindow.setOutsideTouchable(true);
		popupWindow.update();
	}

	public void dissmiss() {
		popupWindow.dismiss();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.commit_pingjia:
			String text = editText.getText().toString().trim();
			if (JaderApplication.isEmpty(text)) {
				JaderApplication.show(context, "输入内容不能为空！");
			} else {
				evalute = new Evalute(0, JaderApplication.user.getUserid(),
						goods.getGoodsid(), JaderApplication.getDate(), text);
				RequestParams params = new RequestParams();
				params.addBodyParameter("type", JaderApplication.INSERT_EVALUTE
						+ "");
				params.addBodyParameter("evalute", new Gson().toJson(evalute));
				sendRequest(url, params);

			}
			break;
		default:
			break;
		}
	}

	public Goods getGoods() {
		return goods;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
	}

	private void sendRequest(String url, RequestParams params) {
		JaderApplication.httpUtils.send(HttpMethod.POST, url, params,
				new RequestCallBack<String>() {

					@Override
					public void onFailure(HttpException arg0, String arg1) {
						JaderApplication.show(context, "网络异常，请稍后");
						listener.onClickOKPop();
						popupWindow.dismiss();
					}

					@Override
					public void onSuccess(ResponseInfo<String> arg0) {
						String result = arg0.result;
						if ("true".equals(result)) {
							JaderApplication.show(context, "评价成功");
						} else {
							JaderApplication.show(context, "操作失败，请重试！");
						}
						popupWindow.dismiss();
						listener.onClickOKPop();
					}
				});
	}
}
